/*
 *@Type SocketClient.java
 * @Desc
 * @Author urmsone urmsone@163.com
 * @date 2024/6/13 13:15
 * @version
 */
package client;

import dto.ActionDTO;
import dto.ActionTypeEnum;
import dto.RespDTO;
import dto.RespStatusTypeEnum;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class SocketClient implements Client {
    private String host;
    private int port;

    public SocketClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public void set(String key, String value) {
        try (Socket socket = new Socket(host, port);
             ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {
            // 传输序列化对象
            ActionDTO dto = new ActionDTO(ActionTypeEnum.SET, key, value);
            oos.writeObject(dto);
            oos.flush();

            // 接收响应数据
            RespDTO resp = (RespDTO) ois.readObject();
//            System.out.println("resp data: "+ resp.toString());
            if (resp.getStatus().equals(RespStatusTypeEnum.SUCCESS)) {
                System.out.println("添加成功！");
            } else {
                System.out.println("添加失败！");
            }

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String get(String key) {
        String value = null;
        try (Socket socket = new Socket(host, port);
             ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {
            // 传输序列化对象
            ActionDTO dto = new ActionDTO(ActionTypeEnum.GET, key, null);
            oos.writeObject(dto);
            oos.flush();

            // 接收响应数据
            RespDTO resp = (RespDTO) ois.readObject();
            value = resp.getValue();


        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return value;
    }

    @Override
    public void rm(String key) {
        try (Socket socket = new Socket(host, port);
             ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {

            // 传输序列化对象
            ActionDTO dto = new ActionDTO(ActionTypeEnum.RM, key, null);
            oos.writeObject(dto);
            oos.flush();

            // 接受响应数据
            RespDTO resp = (RespDTO) ois.readObject();
//            System.out.println("resp data: "+ resp.toString());
            if (resp.getStatus().equals(RespStatusTypeEnum.SUCCESS)) {
                System.out.println("删除成功！");
            } else {
                System.out.println("删除失败！");
            }

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
